<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage core
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: database/database.php 1.0 2010-09-18 16:52 hongbin.hsu $
 */
 
// ------------------------------------------------------------------------

require_once(dirname(__FILE__).'/abstract.php');

class Database
{	
	private static $instance = array();
	private static $config = array();
    private $driver = 'mysql';
    
    //----------------------------------------------------------
    /**
     * 关闭外部实例化
     */
	private function __construct()
	{
		
	}

	//----------------------------------------------------------
	/**
	 * 设置数据库配置文件路径
	 *
	 * @param string $file
	 */
	public static function set_config_ini($config_file = null)
	{
		if (empty($config_file)) {
			$config_file = 'config/database.ini';
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 设置数据库配置文件路径
	 *
	 * @param string $file
	 */
	public static function set_config_file($config_file = null)
	{
		if (empty($config_file)) {
			$config_file = 'config/database.php';
		}
		
		include_once(APP_PATH.$config_file);
		
		self::$config = empty($database) ? array() : $database;
	}
	
	//----------------------------------------------------------
	/**
	 * 取得数据库配置
	 *
	 * @param string $type
	 * @return mixed
	 */
	private static function get_config($type)
	{
		return isset(self::$config[$type]) ? self::$config[$type] : null;
	}
   
    //----------------------------------------------------------
    /**
     * 静态化获得数据驱动实例化
     *
     * @param string $driver
     * @param array $config
     * @return object
     */
    public static function get_instance($type = 'default')
    {
    	if (empty(self::$instance[$type])) {
    		$object = new self();
    		self::$instance[$type] = $object->initialize($type);
    	}
    	return self::$instance[$type];
    }
    
    //----------------------------------------------------------
    /**
     * 获取对应数据驱动实例化
     *
     * @return object
     */
    private function initialize($type){
    	try {
    		$config = self::get_config($type);
    		
	    	if (isset($config['driver'])) {
	    		$this->driver = trim($config['driver']);
	    		unset($config['driver']);		//删除驱动元素
	    	}
	    	$this->driver = strtolower($this->driver);
	    	$driverfile = FIT_PATH."/database/driver/{$this->driver}.php";
	    	require_once($driverfile); 
			$classname = format_string('database_driver_'.$this->driver);
	        if(class_exists($classname) && is_file($driverfile)){
	        	$obj = new $classname($config);
	        }else{
	        	throw new Exception('unfound database driver');
	        }
	        return $obj;
    	}catch (Exception $e){
    		echo $e->getMessage();
    	}
    }
}
